package com.yangye.cloud.demo.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yangye.cloud.demo.pay.domain.Event;
import com.yangye.cloud.demo.pay.domain.Pay;
import com.yangye.cloud.demo.pay.mapper.EventMapper;
import com.yangye.cloud.demo.pay.mapper.PayMapper;
import com.yangye.cloud.demo.pay.service.PayService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author yangye
 * @date 2020/10/30
 */
@Service
public class PayServiceImpl implements PayService {
	@Autowired
	private PayMapper payMapper;
	@Autowired
	private EventMapper eventMapper;

	private ObjectMapper objectMapper = new ObjectMapper();


	@SneakyThrows
	@Override
	@Transactional
	public void paySuccess(Pay payInfo) {
		// 更新支付记录表为支付成功
		payInfo.setName("支付成功");
		payMapper.updateByPrimaryKey(payInfo);

		// 新增事件表
		Event event = new Event();
		event.setEventType((byte) 1);
		event.setEventInfo(JSON.toJSONString(payInfo));
		eventMapper.insert(event);
	}
}
